package com.kevin.training.meal.controller;

import java.util.List;

import javax.inject.Inject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kevin.training.meal.domain.User;
import com.kevin.training.meal.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

	private static Logger logger = Logger.getLogger(UserController.class);

	@Inject
	private UserService userService;

	@RequestMapping(method=RequestMethod.GET)
	public String setupUserRegister(Model model) {
		logger.debug("setupUserRegister");
		model.addAttribute("user", new User());
		return "user/edit";
	}

	@RequestMapping(method=RequestMethod.POST)
	public String submitUserRegister(User user, Model model) {
		userService.register(user);
		model.addAttribute("user", user);
		return "user/edit";
	}

	@RequestMapping(value="/add", method=RequestMethod.POST)
	public @ResponseBody boolean addUser(@RequestBody User user) {
		userService.register(user);
		return true;
	}

	@RequestMapping(value="/detail", method=RequestMethod.GET)
	public @ResponseBody User getUserInfo(@RequestParam int id) {
		User user = userService.getUserInfo(id);
		return user;
	}

	@RequestMapping(value="/list", method=RequestMethod.GET)
	public @ResponseBody List<User> getAllIdAndNames() {
		List<User> userList = userService.getAllIdAndNames();
		return userList;
	}

	@RequestMapping(value="/delete", method=RequestMethod.GET)
	public @ResponseBody boolean delete(@RequestParam int id) {
		userService.delete(id);
		return true;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
